/**
 * Created by Administrator on 2017/12/21.
 */
/**
 * 栏目管理初始化
 */
var Vedio = {
    id: "VedioTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Vedio.initColumn = function () {
    return [
        {field: 'selectItem', checkbox: true},
        {title: 'id', field: 'id', align: 'center', valign: 'middle'},
        {title: '标题', field: 'name', align: 'center', valign: 'middle', sortable: true},
        {title: '所属专辑', field: 'cateName', align: 'center', valign: 'middle', sortable: true},
        {title: '所属年龄', field: 'ageType', align: 'center', valign: 'middle', sortable: true},
        {title: '发布者', field: 'uploader', align: 'center', valign: 'middle', sortable: true},
        {title: '发布时间', field: 'createTime', align: 'center', valign: 'middle', sortable: true},
        {title: '播放量', field: 'playCount', align: 'center', valign: 'middle', sortable: true},
        {title: '分享量', field: 'shareCount', align: 'center', valign: 'middle', sortable: true},
        {title: '下载量', field: 'downloadCount', align: 'center', valign: 'middle', sortable: true},
        {title: '推荐排序值', field: 'recRate', align: 'center', valign: 'middle', sortable: true},
        {title: '推荐指数', field: 'rating', align: 'center', valign: 'middle', sortable: true}
    ];
};

/**
 * 检查是否选中
 */
Vedio.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Vedio.seItem = selected[0];
        if(Vedio.seItem == 'undefined' || Vedio.seItem == '' || Vedio.seItem == null) {
            Vedio.seItem = selected[1];
        }
        return true;
    }
};
Vedio.batchCheck = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Vedio.seItem = selected;
        return true;
    }
};

/**
 * 点击添加栏目
 */
Vedio.openAddVedio = function () {
    var index = layer.open({
        type: 2,
        title: '视频添加',
        area: ['900px', '600px'], //宽高
        fix: false, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/vedio/video/vedioAdd'
    });
    this.layerIndex = index;
};

/**
 * 点击excel批量
 */
Vedio.openExcelAdd = function () {
    location.href=Feng.ctxPath + "/vedio/video/batch";
    // var index = layer.open({
    //     type: 2,
    //     title: 'excel添加',
    //     area: ['600px', '200px'], //宽高
    //     fix: false, //不固定
    //     maxmin: true,
    //     content: Feng.ctxPath + '/vedio/video/toExcelAdd'
    // });
    // this.layerIndex = index;
};

/**
 * 打开查看栏目详情
 */
Vedio.openVedioDetail = function () {
    if (this.check()) {
        var index = layer.open({
            type: 2,
            title: '视频修改',
            area: ['900px', '600px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/vedio/video/vedioUpdate/' + Vedio.seItem.id
        });
        this.layerIndex = index;
    }
};

Vedio.batchUp = function () {
    if(this.batchCheck()) {
        var index = layer.open({
            type: 2,
            title: '批量修改',
            area: ['600px', '300px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/vedio/video/batchUpPage'
        });
        this.layerIndex = index;
    }
};

/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
Vedio.formParams = function() {
    var queryData = {};

    queryData['keyword'] = $("#keyword").val();
    queryData['beginTime'] = $("#beginTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['searchType'] = $("#searchType option:selected").val();
    return queryData;
};

/**
 * 查询日志列表
 */
Vedio.search = function () {
    Vedio.table.refresh({query: Vedio.formParams()});
};

Vedio.resetSearch = function () {
    $("#keyword").val("");
    $("#beginTime").val("");
    $("#endTime").val("");
    $("#searchType option").each(function () {
        $(this).removeAttr('selected');
    });
    Vedio.search();
}

Vedio.excelDownload = function () {
    window.top.open(Feng.ctxPath + "/vedio/video/excelDownload");
    setTimeout(function () {
        window.top.close();
    },1000)
};

/**
 * 删除栏目
 */
Vedio.delete = function () {
    if (this.batchCheck()) {

        var operation = function(){
            var ajax = new $ax(Feng.ctxPath + "/vedio/video/delete", function (data) {
                Feng.success("删除成功!");
                Vedio.table.refresh();
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            var ids = '';
            console.log(Vedio.seItem);
            for (var i = 0; i < Vedio.seItem.length;i++) {
               if(i < Vedio.seItem.length - 1) {
                   ids += Vedio.seItem[i].id+",";
               } else {
                   ids += Vedio.seItem[i].id;
               }
            }
            ajax.set("vedioIds", ids);
            ajax.start();
        };

        var names = "";
        for (var i = 0; i < Vedio.seItem.length;i++) {
            if(i < Vedio.seItem.length - 1) {
                names += Vedio.seItem[i].name+" ";
            } else {
                names += Vedio.seItem[i].name;
            }
        }
        Feng.confirm("是否删除栏目 " + names + "?", operation);
    }
};

$(function () {
    var defaultColunms = Vedio.initColumn();
    var table = new BSTable(Vedio.id, "/vedio/video/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(Vedio.formParams());
    Vedio.table = table.init();
});

